package IR;

import java.io.IOException;

import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.util.AttributeSource;

public class ProperNameTokenFilter extends TokenFilter {

  private CharTermAttribute termAttr;
  private AttributeSource.State current;
    
  protected ProperNameTokenFilter(TokenStream input)
  {
    super(input);
    this.termAttr = addAttribute(CharTermAttribute.class);
  }
  private String trimB(String str)
  {
      int i = 0;
      int flag = 1;
      while (i < str.length() && flag == 1)
      {
          if ((str.charAt(i) < 48) || (str.charAt(i) > 122) || (str.charAt(i) > 90 && str.charAt(i) < 97) || (str.charAt(i) > 57 && str.charAt(i) < 65))
          {
              str = str.substring(i + 1);
              i--;
          }
          else
              flag = 0;
          i++;
      }
      if (str.length() == 0)
          str = null;
      return str;
  }
  private String trimE(String str)
  {
      int i = str.length() - 1;
      int flag = 1;
      while (i > 0 && flag == 1)
      {
          if ((str.charAt(i) < 48) || (str.charAt(i) > 122) || (str.charAt(i) > 90 && str.charAt(i) < 97) || (str.charAt(i) > 57 && str.charAt(i) < 65))
              str = str.substring(0, i);
          else
              flag = 0;
          i--;
      }
      return str;
  }
  
  private boolean verif(String str)
  {
      int i = 1;
      while (i < str.length())
      {
          if (str.charAt(i) < 97 || str.charAt(i) > 122)
            return false;
          i++;
      }
      return true;
  }

  @Override
  public boolean incrementToken() throws IOException
  {
    String term = null;
    String termTmp = null;
    String save = null;
    while (input.incrementToken())
    {
      termTmp = new String(termAttr.buffer(), 0, termAttr.length());
      term = trimB(termTmp);
      if (current != null)
      {
          restoreState(current);
          save = new String(termAttr.buffer(), 0, termAttr.length());
          save = trimE(trimB(save));
          current = null;
      }
      if (term != null)
      {
          if (term.charAt(0) < 65 || term.charAt(0) > 90)
          {
              save = null;
              term = trimE(term);
              termAttr.copyBuffer(term.toCharArray(), 0, term.length());
              termAttr.setLength(term.length());
              return true;
          }
          else
          {
              if (save == null)
              {
                  if (verif(term))
                  {
                    current = captureState();
                  }
                term = trimE(term);
                termAttr.copyBuffer(term.toCharArray(), 0, term.length());
                termAttr.setLength(term.length());
                return true;
              }
              else if (verif(trimE(termTmp)))
              {
                term = trimE(term);
                term = save + " " + term;
                termAttr.copyBuffer(term.toCharArray(), 0, term.length());
                termAttr.setLength(term.length());
                save = null;
                return true;
              }
              else
              {
                term = trimE(term);
                termAttr.copyBuffer(term.toCharArray(), 0, term.length());
                termAttr.setLength(term.length());
                return true;
              }
          }
      }
    }
    return false;
  }
}